package com.tidemark.robot;

import java.util.Date;
import java.util.Timer;
import java.util.Scanner;


/**
 * 1 robot has 1 google account with N RegularTasks
 * 
 * 1 RegularTask operates on 1 portfolio
 */
public class Robot {

	  private static void printUsage() {
		    System.out.println("  Usage:");
		    System.out.println("    FinancePortfoliosClient <account> <password>");
		    System.out.println("  Example:");
		    System.out.println("    FinancePortfoliosClient " +
		                       " user@gmail.com password");
		  }
	  
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
	    if (args.length != 0 && args.length != 2) {
	        printUsage();
	        System.exit(0);
	      }
	      // Set username and password from command-line arguments if they were passed
	      // in. Otherwise prompt the user to login.
	      if (args.length == 2) {
	        Account.ID = args[0];
	        Account.PWD = args[1];
	      } else {
	        System.out.println("Enter user ID: ");
	        Account.ID = sc.nextLine();
	        System.out.println("Enter user password: ");
	        Account.PWD = sc.nextLine();
	      }		
		
		new Robot();
	}

	/**
	 * TODO because one robot can have multiple portfolios, 
	 * is seems not useful to create multiple Robots...
	 */
	private Robot() {
		
		Date start = new Date();
      // start.setHours(Parameters.START_HOUR); //every day 14:50pm
		// start.setMinutes(Parameters.START_MINUTE);

		Timer timer = new Timer(Account.ID, false);//TODO is daemon useful?

//		timer.scheduleAtFixedRate(new RegularTask("30Sec"), start, 1000 * 30);

		timer.scheduleAtFixedRate(new RegularTask("Daily"), start, 1000 * 60 * 60 * 24);
		
//TODO 避免总是周末 可以考虑 每周一，二...做五个		
//		timer.scheduleAtFixedRate(new RegularTask("Weekly"), start, 1000 * 60
//				* 60 * 24 * 7);

		System.out.println("Robot is started.");
	}

//	/**
//	 * the closest coming trade day after the given date
//	 * 
//	 * @param from
//	 *            from which day on,
//	 * @return the closest trade day after give date
//	 */
//	private Date getFirstTradeDay(Date from) {
//		if (from == null || from.getDate() < new Date().getDate()) {// if
//																	// invalid
//																	// param
//			return getFirstTradeDay(new Date()); // use today re-calculate
//		} else {// today or future date
//			if (isTradeDay(from)) {// check if trade day
//				return from;// if yes, schedule it
//			} else { // if not trade day
//				from.setDate(from.getDate() + 1);// set to the next day
//				return getFirstTradeDay(from);// and check again
//			}
//		}
//	}
//
//	private boolean isTradeDay(Date date) {
//		int day = date.getDay();
//		if (day == 6 || day == 7) {
//			return false; // given date is weekend
//		}
//		// TODO how to check holidays
//
//		if (date.getDate() == (new Date().getDate())) {// if today
//			return isTodayOk();
//		}
//		return true;
//	}
//
//	private boolean isTodayOk() {
//		int hour = new Date().getHours();
//		if (hour >= 15) {
//			return false;// today is too late
//		}
//		return true;
//	}
	
}
